package com.hundsun.cprs.yyt.common.fileUpload;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import com.hundsun.cprs.yyt.service.classify.domain.Classify;
import com.hundsun.cprs.yyt.service.plate.domain.Plate;
import com.hundsun.cprs.yyt.service.plate.domain.PlateImage;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.multipart.MultipartFile;

import com.hundsun.cprs.yyt.service.classify.domain.Classify;
import com.hundsun.cprs.yyt.service.plate.domain.Plate;
import com.hundsun.cprs.yyt.service.plate.domain.PlateImage;
import com.hundsun.cprs.yyt.util.DateUtil;
import com.hundsun.cprs.yyt.util.ImgUtils;
import com.hundsun.cprs.yyt.util.UploadUtil;
import com.hundsun.jresplus.common.util.StringUtil;

import javax.imageio.ImageIO;


public class FileUploadUtil {

	private Log log = LogFactory.getLog(getClass());
	public static final String SEP = "/";
	public static final String POINT = ".";
	public static final String[] EXTARRAY = { "jpg", "jpeg", "gif", "png" };
	public static final int MAX_IMAG_SIZE = 5 * 1024 * 1024; // 最大图片文件尺寸5M

	public String uploadRoot;

	public static int MAX_FILE_SIZE = 3072 * 1024; //文件最大为3M

	public static final String CRM_PIC_PATH = "000000";

	// 小图片高度
	private static int smallImgHeight = 60;
	// 小图片宽度
	private static int smallImgWidth = 60;
	// 小图片高度
	private static int smallGoodsImgHeight = 0;
	// 小图片宽度
	private static int smallGoodsImgWidth = 0;
	//logo图片高度
	private static int smallLogoImgHeight = 88;
	//logo图片宽度
	private static int smallLogoImgWidth = 88;
	// 大图片高度
	private static int bigImgHeight = 460;
	// 大图片宽度
	private static int bigImgWidth = 460;
	// 大图片高度
	private static int bigGoodsImgHeight = 0;
	// 大图片宽度
	private static int bigGoodsImgWidth = 0;
	//一图片高度
	private static int bigOneImgHeight = 211;
	//一图片宽度
	private static int bigOneImgWidth = 500;
	//二图片高度
	private static int smallTwoImgHeight = 126;
	//二图片宽度
	private static int smallTwoImgWidth = 180;

	/*@Value("${upload.root.licence.url.path}")
	public String uploadUrl;

	*//**
	 * 会员等级上传目录
	 *//*
	public static final String MEMLEVEL_FILE_HOME_DIR = "memlevel";

	*//**
	 * 会员信用上传目录
	 *//*
	public static final String USERCREDIT_FILE_HOME_DIR = "usercredit";

	public String uploadFile(MultipartFile file, String userId, String destDir) {
		if (StringUtil.isBlank(userId)) {
			return UploadUtil.uploadFile(file, uploadUrl + SEP + destDir);
		}
		return UploadUtil.uploadFile(file, userId + SEP + destDir);
	}*/

	public static String getSubFileDir() {
		return DateUtil.getDateTime("yyyyMMdd", new Date());
	}

	/**
	 * 竞购图片上传
	 *
	 * @param img
	 * @param file
	 * @param destDir
	 * @return
	 * @throws Exception
	 */
	/*public String uploadFile(GoodsImage img, MultipartFile file, String destDir) {
		String filePath = "";
		if (file == null || StringUtil.isEmpty(file.getOriginalFilename())) {
			return filePath;
		}

		String orgFileName = file.getOriginalFilename();
		String exts = orgFileName.substring(orgFileName.lastIndexOf(".") + 1, orgFileName.length());

		if (!UploadUtil.ifExtendNamePermitted(exts, FileUploadUtil.EXTARRAY)) {
			return filePath;
		}

		String newFileName = UploadUtil.createFileName(exts);
		String saveDir = destDir + SEP + getSubFileDir();
		try {
			UploadUtil.saveFile(file, uploadRoot + SEP + saveDir, newFileName);
		} catch (IOException e) {
			log.error("FileUploadUtil.uploadFile", e);
		}
		img.setOriginalImageUrl(saveDir + SEP + newFileName);
		try {
			BufferedImage image = ImageIO.read(file.getInputStream());
			if (image != null) {//如果image=null 表示上传的不是图片格式
				smallGoodsImgWidth = (image.getWidth())/4;//获取小图宽度，单位px
				smallGoodsImgHeight = (image.getHeight())/4;//获取小图高度，单位px
				bigGoodsImgWidth = (image.getWidth())/2;//获取大图宽度，单位px
				bigGoodsImgHeight = (image.getHeight())/2;//获取大图高度，单位px
			}
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "small" + newFileName, smallGoodsImgWidth, smallGoodsImgHeight);
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "big" + newFileName, bigGoodsImgWidth, bigGoodsImgHeight);
			img.setSmallImageUrl(saveDir + SEP + "small" + newFileName);
			img.setBigImageUrl(saveDir + SEP + "big" + newFileName);
		} catch (Exception e) {
			log.error("reduce image failed.", e);
		}
		filePath = destDir + SEP + getSubFileDir() + SEP + newFileName;
		return filePath;
	}*/
	/**
	 * 专场logo上传
	 * @param img
	 * @param file
	 * @param destDir
	 * @return
	 */
	public String uploadFile(Plate img, MultipartFile file, String destDir) {
		String filePath = "";
		if (file == null || StringUtil.isEmpty(file.getOriginalFilename())) {
			return filePath;
		}

		String orgFileName = file.getOriginalFilename();
		String exts = orgFileName.substring(orgFileName.lastIndexOf(".") + 1, orgFileName.length());

		if (!UploadUtil.ifExtendNamePermitted(exts, FileUploadUtil.EXTARRAY)) {
			// 如果文件扩展名不在允许范围内
			return filePath;
		}

		String newFileName = UploadUtil.createFileName(exts);
		String saveDir = destDir + SEP + getSubFileDir();
		try {
			UploadUtil.saveFile(file, uploadRoot + SEP + saveDir, newFileName);
		} catch (IOException e) {
			log.error("FileUploadUtil.uploadFile", e);
		}
		img.setOriginalImageUrl(saveDir + SEP + newFileName);
		try {
			BufferedImage image = ImageIO.read(file.getInputStream());
			if (image != null) {//如果image=null 表示上传的不是图片格式
				smallGoodsImgWidth = (image.getWidth())/4;//获取小图宽度，单位px
				smallGoodsImgHeight = (image.getHeight())/4;//获取小图高度，单位px
				bigGoodsImgWidth = (image.getWidth())/2;//获取大图宽度，单位px
				bigGoodsImgHeight = (image.getHeight())/2;//获取大图高度，单位px
			}
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "small" + newFileName, smallGoodsImgWidth, smallGoodsImgHeight);
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "big" + newFileName, bigGoodsImgWidth, bigGoodsImgHeight);
			img.setSmallImageUrl(saveDir + SEP + "small" + newFileName);
			img.setBigImageUrl(saveDir + SEP + "big" + newFileName);
		} catch (Exception e) {
			log.error("reduce image failed.", e);
		}
		filePath = destDir + SEP + getSubFileDir() + SEP + newFileName;
		return filePath;
	}
	/**
	 * 专场图片上传
	 * @param img
	 * @param file
	 * @param destDir
	 * @return
	 */
	public String uploadFile(PlateImage img, MultipartFile file, String destDir) {
		String filePath = "";
		if (file == null || StringUtil.isEmpty(file.getOriginalFilename())) {
			return filePath;
		}

		String orgFileName = file.getOriginalFilename();
		String exts = orgFileName.substring(orgFileName.lastIndexOf(".") + 1, orgFileName.length());

		if (!UploadUtil.ifExtendNamePermitted(exts, FileUploadUtil.EXTARRAY)) {
			// 如果文件扩展名不在允许范围内
			return filePath;
		}

		String newFileName = UploadUtil.createFileName(exts);
		String saveDir = destDir + SEP + getSubFileDir();
		try {
			UploadUtil.saveFile(file, uploadRoot + SEP + saveDir, newFileName);
		} catch (IOException e) {
			log.error("FileUploadUtil.uploadFile", e);
		}
		img.setOriginalImageUrl(saveDir + SEP + newFileName);
		try {
			BufferedImage image = ImageIO.read(file.getInputStream());
			if (image != null) {//如果image=null 表示上传的不是图片格式
				smallGoodsImgWidth = (image.getWidth())/4;//获取小图宽度，单位px
				smallGoodsImgHeight = (image.getHeight())/4;//获取小图高度，单位px
				bigGoodsImgWidth = (image.getWidth())/2;//获取大图宽度，单位px
				bigGoodsImgHeight = (image.getHeight())/2;//获取大图高度，单位px
			}
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "small" + newFileName, smallGoodsImgWidth, smallGoodsImgHeight);
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "big" + newFileName, bigGoodsImgWidth, bigGoodsImgHeight);
			img.setSmallImageUrl(saveDir + SEP + "small" + newFileName);
			img.setBigImageUrl(saveDir + SEP + "big" + newFileName);
		} catch (Exception e) {
			log.error("reduce image failed.", e);
		}
		filePath = destDir + SEP + getSubFileDir() + SEP + newFileName;
		return filePath;
	}
	/**
	 * 分类上传
	 *
	 * @param img
	 * @param file
	 * @param destDir
	 * @return
	 * @throws Exception
	 */
	public String uploadFile(Classify img, MultipartFile file, String destDir) {
		String filePath = "";
		if (file == null || StringUtil.isEmpty(file.getOriginalFilename())) {
			return filePath;
		}

		String orgFileName = file.getOriginalFilename();
		String exts = orgFileName.substring(orgFileName.lastIndexOf(".") + 1, orgFileName.length());

		if (!UploadUtil.ifExtendNamePermitted(exts, FileUploadUtil.EXTARRAY)) {
			return filePath;
		}

		String newFileName = UploadUtil.createFileName(exts);
		String saveDir = destDir + SEP + getSubFileDir();
		try {
			UploadUtil.saveFile(file, uploadRoot + SEP + saveDir, newFileName);
		} catch (IOException e) {
			log.error("FileUploadUtil.uploadFile", e);
		}
		img.setOriginalImageUrl(saveDir + SEP + newFileName);
		try {
			BufferedImage image = ImageIO.read(file.getInputStream());
			if (image != null) {//如果image=null 表示上传的不是图片格式
				smallGoodsImgWidth = (image.getWidth())/4;//获取小图宽度，单位px
				smallGoodsImgHeight = (image.getHeight())/4;//获取小图高度，单位px
				bigGoodsImgWidth = (image.getWidth())/2;//获取大图宽度，单位px
				bigGoodsImgHeight = (image.getHeight())/2;//获取大图高度，单位px
			}
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "small" + newFileName, smallGoodsImgWidth, smallGoodsImgHeight);
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "big" + newFileName, bigGoodsImgWidth, bigGoodsImgHeight);
			img.setSmallImageUrl(saveDir + SEP + "small" + newFileName);
			img.setBigImageUrl(saveDir + SEP + "big" + newFileName);
		} catch (Exception e) {
			log.error("reduce image failed.", e);
		}
		filePath = destDir + SEP + getSubFileDir() + SEP + newFileName;
		return filePath;
	}
	/**
	 * 板块图片上传
	 * @return
	 *//*
	public String uploadFile(ProjectOrganization img, MultipartFile file, String destDir) {
		String filePath = "";
		if (file == null || StringUtil.isEmpty(file.getOriginalFilename())) {
			return filePath;
		}
		
		String orgFileName = file.getOriginalFilename();
		String exts = orgFileName.substring(orgFileName.lastIndexOf(".") + 1, orgFileName.length());
		
		if (!UploadUtil.ifExtendNamePermitted(exts, FileUploadUtil.EXTARRAY)) {
			// 如果文件扩展名不在允许范围内
			return filePath;
		}
		
		String newFileName = UploadUtil.createFileName(exts);
		String saveDir = destDir + SEP + getSubFileDir();
		try {
			UploadUtil.saveFile(file, uploadRoot + SEP + saveDir, newFileName);
		} catch (IOException e) {
			log.error("FileUploadUtil.uploadFile", e);
		}
		img.setOriginalImageUrl(saveDir + SEP + newFileName);
		try {
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "small" + newFileName, smallImgWidth, smallImgHeight);
			ImgUtils.reduceImg(uploadRoot + SEP + saveDir + SEP + newFileName,
					uploadRoot + SEP + saveDir + SEP + "big" + newFileName, bigImgWidth, bigImgHeight);
			img.setSmallImageUrl(saveDir + SEP + "small" + newFileName);
			img.setBigImageUrl(saveDir + SEP + "big" + newFileName);
		} catch (Exception e) {
			log.error("reduce image failed.", e);
		}
		filePath = destDir + SEP + getSubFileDir() + SEP + newFileName;
		return filePath;
	}*/

	public String getUploadRoot() {
		return uploadRoot;
	}

	public void setUploadRoot(String uploadRoot) {
		this.uploadRoot = uploadRoot;
	}


}
